<?php

class FinishedorderController extends BaseController {


//finished oerder
		public function getFinishedorder()
	{	
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('dateby');
		Session::forget('datestart');
		Session::forget('dateend');
		$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);


		return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);

	}

	public function postFinishedorder()
	{	

		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			Session::put('dateby',Input::get('dateby'));

			if(Input::get('dateby')=='all'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateorder'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_status_type','=','Pending')
						->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateapprove'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_status_type','=','Approve')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateprocess'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_status_type','=','On Process')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='datefinished'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_status_type','=','Finished')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}


			return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::forget('dateby');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else{
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_name','=',Input::get('typeby'))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::forget('dateby');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='code'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('order_code','LIKE','%'.Input::get('keyword').'%')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);
		}else if(Input::get('filterby')=='name'){
			$finishedorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Finished')
						->where('customer_name','LIKE','%'.Input::get('keyword').'%')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);
		}
		}else{
        	if(Session::get('filter')=="date"){
        			if(Session::get('dateby')=='all'){
					$finishedorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Finished')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateorder'){
					$finishedorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Finished')
								->where('order_status_type','=','Pending')
								->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateapprove'){
					$finishedorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Finished')
								->where('order_status_type','=','Approve')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateprocess'){
					$finishedorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Finished')
								->where('order_status_type','=','On Process')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='datefinished'){
					$finishedorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Finished')
								->where('order_status_type','=','Finished')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}
        	}else if(Session::get('filter')=="type"){
					if(Session::get('list')=='all'){
					$finishedorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Finished')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else{
					$finishedorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Finished')
									->where('order_name','=',Session::get('list'))
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}else if(Session::get('filter')=="keyword"){
        			if(Session::get('filterby')=='code'){
						$finishedorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Finished')
									->where('order_code','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else if(Session::get('filterby')=='name'){
						$finishedorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Finished')
									->where('customer_name','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
			}
		}
		return View::make('admin.order.finished.finishedorder')->with('finishedorder',$finishedorder);
		



	}

	public function postFinishedordersetpayment($id)
	{	
		$cust = DB::table('sys_order')
					->join('sys_customer','order_customer_id','=','customer_id')
					->where('order_id',$id)
					->first();
		$user = array(
			  'email'=>$cust->customer_email,
			  'nama'=>$cust->customer_name
			);
		$invoice = DB::table('sys_invoice')
					->where('invoice_order_id','=',$id)
					->first();
		$kekurangan = DB::table('sys_order')
                      ->join('sys_invoice','invoice_order_id','=','order_id')
                      ->join('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
                      ->where('order_id',$id)
                      ->sum('invoice_detail_payment');

			if($invoice->invoice_total_payment==$kekurangan){
				$lunas = DB::table('sys_order')
                      ->join('sys_invoice','invoice_order_id','=','order_id')
					  ->where('order_id','=',$id)
					  ->where('order_status','=','Finished')
                      ->update(array('invoice_status' => 'Lunas'));

                      return Redirect::to('/order/finishedorder')->with('message','Pembayaran telah lunas');
			}else{

		if(Input::get('termin4')<>0){
			$term3 = DB::table('sys_invoice')
                    ->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
					->where('invoice_order_id','=',$id)
                    ->where('invoice_detail_status','=','Termin III')
                    ->count();
			if($term3==0){
				return Redirect::to('/order/finishedorder')->with('error','Pembayaran tidak sesuai dan tidak urut');
			}
		}

		if(Input::get('termin3')<>0){
			$term2 = DB::table('sys_invoice')
                    ->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
					->where('invoice_order_id','=',$id)
                    ->where('invoice_detail_status','=','Termin II')
                    ->count();
			if($term2==0){
				return Redirect::to('/order/finishedorder')->with('error','Pembayaran tidak urut dan tidak sesuai');
			}
		}

		
		$pay = $invoice->invoice_total_payment-$kekurangan;
		if((Input::get('pelunasan')<>$pay) AND (Input::get('pelunasan')<>0)){
			return Redirect::to('/order/finishedorder')->with('error','Pembayaran tidak lunas');
		}
		$termin = Input::get('termin2')+Input::get('termin3')+Input::get('termin4')+Input::get('pelunasan');
		if($termin>$pay){
			return Redirect::to('/order/finishedorder')->with('error','Pembayaran tidak sesuai');
		}
		$invoicedp = New invoicedetail();
		$invoicedp->invoice_detail_invoice_id = $invoice->invoice_id;
		if(Input::get('termin2')<>0){
			$invoicedp->invoice_detail_payment = Input::get('termin2');
			$invoicedp->invoice_detail_status = 'Termin II';

			
			//send mail
			Mail::send('emails.pesananpembayaran', array('pesan'=>'Pembayaran telah kami terima, Terimakasih.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pembayaran Termin II kami terima');
	    	});
		}
		if(Input::get('termin3')<>0){
			$invoicedp->invoice_detail_payment = Input::get('termin3');
			$invoicedp->invoice_detail_status = 'Termin III';
			//send mail
			Mail::send('emails.pesananpembayaran', array('pesan'=>'Pembayaran telah kami terima, Terimakasih.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pembayaran Termin III kami terima');
	    	});

		}
		if(Input::get('termin4')<>0){
			$invoicedp->invoice_detail_payment = Input::get('termin4');
			$invoicedp->invoice_detail_status = 'Termin IV';
			//send mail
			Mail::send('emails.pesananpembayaran', array('pesan'=>'Pembayaran telah kami terima, Terimakasih.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pembayaran Termin IV kami terima');
	    	});

		}
		if(Input::get('pelunasan')<>0){
			$invoicedp->invoice_detail_payment = Input::get('pelunasan');
			$invoicedp->invoice_detail_status = 'Pelunasan';
			//send mail
			Mail::send('emails.pesananlunas', array('pesan'=>'Pembayaran telah kami terima, Terimakasih.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pembayaran kami terima, Terimakasih');
	    	});

		}
		$invoicedp->invoice_detail_date = date('Y-m-d H:i:s');
		$invoicedp->save();

			}


			$invoice = DB::table('sys_invoice')
					->where('invoice_order_id','=',$id)
					->first();
			$kekurangan = DB::table('sys_order')
	                      ->join('sys_invoice','invoice_order_id','=','order_id')
	                      ->join('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
	                      ->where('order_id',$id)
	                      ->sum('invoice_detail_payment');

				if($invoice->invoice_total_payment==$kekurangan){
					$lunas = DB::table('sys_order')
	                      ->join('sys_invoice','invoice_order_id','=','order_id')
						  ->where('order_id','=',$id)
						  ->where('order_status','=','Finished')
	                      ->update(array('invoice_status' => 'Lunas'));
	        }

	       				 $log = New adminlog();
						$log->admin_log_admin_id = Session::get('admin_id');	
						$log->admin_log_activity = "Set pembayaran order : ".$id;	
						$log->admin_log_date = date('Y-m-d H:i:s');	
						$log->save();
			return Redirect::to('/order/finishedorder')->with('message','Pembayaran telah diterima');

	}

	public function postFinishedorderdelivered()
	{
		$orders = DB::table('sys_order')
			    ->join('sys_customer','order_customer_id','=','customer_id')
				->join('sys_invoice','invoice_order_id','=','order_id')
				->get();
		$a = 0;
		foreach($orders as $order){
		
		if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);

			if($order->invoice_status=='Belum lunas'){
				return Redirect::to('/order/finishedorder')->with('error','Pesanan belum lunas');
				}
		
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Delivered'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Delivered';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set order : ".$order->order_id." Delivered";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesanandelivered', array('pesan'=>'Pesanan anda telah selesai dan terkirim, Terimakasih.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pesanan terkirim');
	    	});
			}

		}
		if($a<>0){
			return Redirect::to('/order/finishedorder')->with('message','Order telah delivered');
		}
		return Redirect::to('/order/finishedorder')->with('error','Silahkan pilih order');
		
	}

	public function postFinishedordercancel()
	{
		$orders = DB::table('sys_order')
				->get();
		$a = 0;
		foreach($orders as $order){
		if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'On Process'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'On Process';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$order->order_id." On Process";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

		}
		}
		if($a<>0){
			return Redirect::to('/order/finishedorder')->with('message','Order telah dicancel dari daftar proses');
		}
		return Redirect::to('/order/finishedorder')->with('error','Silahkan pilih order');
		
	}



}